import { getListApi } from "@/api/configCenter/outBoundAgent";
import { getAllAccountTreeApi } from "@/api/financialManagement/receivable/waterVerification";
import { getAllCompanyApi } from "@/api/managementCenter/departmentPersonnel";
import { reactive, ref } from "vue";

export default function InitHook() {
  const formDataRef = ref<any>(null);
  const formState = reactive<PaymentRegistration.AddPaymentWater>({
    companyId: undefined,
    agentId: undefined,
    payAccountId: [],
    payAmount: undefined,
    payTime: undefined,
    serialNumber: undefined,
    remark: undefined
  });
  const loading = ref<boolean>(false);
  // 经营单位
  const companyOpts = ref<Base.GeneraDataType[]>([]);
  const getAllCompany = () => {
    return getAllCompanyApi({ status: 1, pageNum: 1, pageSize: 9999999 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        companyOpts.value = data || [];
      }
    });
  };
  // 出仓代理
  const agentOpts = ref<Base.GeneraDataType[]>([]);
  const getAllAgent = () => {
    return getListApi({
      status: 1,
      pageNum: 1,
      pageSize: 999999
    } as OutBoundAgent.SearchForm).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        agentOpts.value = data.records || [];
      }
    });
  };
  // 付款账户
  const paymentAccountOpts = ref<Base.GeneraDataType[]>([]);
  const getAllPaymentAccount = () => {
    return getAllAccountTreeApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        paymentAccountOpts.value = data.map((t: any) => {
          return {
            name: t.deptName,
            id: t.deptId,
            ...t
          };
        });
      }
    });
  };
  return {
    formDataRef,
    formState,
    loading,
    getAllCompany,
    companyOpts,
    agentOpts,
    getAllAgent,
    paymentAccountOpts,
    getAllPaymentAccount
  };
}
